package leetcode;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;

public class LC15 {
    public static void main(String[] args) {
        LC15 lc15 = new LC15();
        lc15.rotate(new int[]{-1,-100,3,99},2);
        lc15.rotate(new int[]{1,2,3,4,5,6,7},3);
        lc15.rotate(new int[]{1,2},3);
    }
    public void rotate(int[] nums, int k) {
        for (int i = 0; i < k; i++) {
            int m = 0;
            int j = i+k+m;
            while(j != i){
                int temp = nums[j];
                nums[j] = nums[i];
                nums[i] = temp;
                j = (i+k+m++)%nums.length;
            }
        }
        System.out.println(Arrays.toString(nums));
    }

    public void rotate1(int[] nums, int k) {
        LinkedList<Integer> queue = new LinkedList<>();
        for (int num : nums) {
            queue.add(num);
        }
        for(int i = 0 ; i < k; i++){
            Integer x = queue.pollLast();
            if(x != null)
                queue.addFirst(x);
        }
        for (int i = 0; i < nums.length; i++) {
            nums[i] = queue.poll();
        }
        System.out.println(Arrays.toString(nums));
    }
}
